<?php
function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
return;
}

require_once('lib/db_connection.php');
$company_id = $_SESSION['company_id'];
$p1 = $_GET['p1'];
$p2 = $_GET['p2'];
$p3 = $_GET['p3'];

if($p2=='LIKE')
$passed_where = $p1." ".$p2." '%".$p3."%'";
if($p2!='LIKE')
$passed_where = $p1." ".$p2." '".$p3."'";

	$filename = "Expense_Report".date('Y-m-d');
    // Send Header
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$filename.xls "); // แล้วนี่ก็ชื่อไฟล์
    header("Content-Transfer-Encoding: binary ");

	
	
   // Query Database
    if($p3==''||$p3==null)
	{
	$query_exp = "SELECT 
	veh_expense_tbl.*,veh_expense_tbl.vehicle_id as v_num,
	vehicle_tbl.*,vehicle_tbl.vehicle_id as r_id 
	FROM veh_expense_tbl 
	JOIN vehicle_tbl 
	ON veh_expense_tbl.vehicle_id=vehicle_tbl.vehicle_id
	WHERE veh_expense_tbl.company_id='".$company_id."'";
	}

	if($p3!=''||$p3!=null)
	{
	$query_exp = "SELECT 
	veh_expense_tbl.*,veh_expense_tbl.vehicle_id as v_num,
	vehicle_tbl.*,vehicle_tbl.vehicle_id as r_id 
	FROM veh_expense_tbl 
	JOIN vehicle_tbl 
	ON veh_expense_tbl.vehicle_id=vehicle_tbl.vehicle_id
	WHERE veh_expense_tbl.company_id='".$company_id."' AND ".$passed_where;	
	}
	$result_exp = mysql_query($query_exp)or die(mysql_error());
   
    // XLS Data Cell

                xlsBOF();
                /*xlsWriteLabel(1,0,"Student Register $semester/$year");
                xlsWriteLabel(2,0,"COURSENO : ");
                xlsWriteLabel(2,1,"$courseid");
                xlsWriteLabel(3,0,"TITLE : ");
                xlsWriteLabel(3,1,"$title");
                xlsWriteLabel(4,0,"SETION : ");
                xlsWriteLabel(4,1,"$sec");
                xlsWriteLabel(6,0,"Plate");
                xlsWriteLabel(6,1,"Model");
                xlsWriteLabel(6,2,"Manufacturer");
                xlsWriteLabel(6,3,"Track By");
                xlsWriteLabel(6,4,"Odometer/Hrs");
                $xlsRow = 7;*/
                xlsWriteLabel(0,0,"Plate");
                xlsWriteLabel(0,1,"Year");
                xlsWriteLabel(0,2,"Make");
                xlsWriteLabel(0,3,"Model");
                xlsWriteLabel(0,4,"Date");
                xlsWriteLabel(0,5,"Expense Type");
                xlsWriteLabel(0,6,"Vendor");
                xlsWriteLabel(0,7,"Description");
                xlsWriteLabel(0,8,"Amount");
            
                $xlsRow = 1;
                while($row_exp=mysql_fetch_array($result_exp)) {
                    ++$i;
                    
            	//TRACKING
				/*$tracking = $row_ws['track_by'];
				if($tracking=='distance')
				$curr_od = $row_ws['odometer'].'Km';
				if($tracking=='hours')
				$curr_od = $row_ws['odometer'].'Hrs';*/
                
				//xlsWriteNumber
				xlsWriteLabel($xlsRow,0,$row_exp['veh_num']);
                xlsWriteLabel($xlsRow,1,$row_exp['year']);
                xlsWriteLabel($xlsRow,2,$row_exp['manufacturer_name']);
                xlsWriteLabel($xlsRow,3,$row_exp['model_name']);    
                xlsWriteLabel($xlsRow,4,$row_exp['date']);
                xlsWriteLabel($xlsRow,5,$row_exp['type']);
                xlsWriteLabel($xlsRow,6,$row_exp['vendor']);
                xlsWriteLabel($xlsRow,7,$row_exp['comments']);
                xlsWriteLabel($xlsRow,8,$row_exp['amount']);
                $xlsRow++;
				}                    
                xlsEOF();
                exit();
?>